﻿using System;
using System.Collections.Generic;
using System.IO;
using Bookvar.ApplicationModel;
using Bookvar.ObjectModel;
using Bookvar.ServiceModel;

namespace Bookvar.Common
{
    public class FileSystemPersistenceService : IPersistenceService
    {
        private readonly Dictionary<Guid, FileSystemPersister> registeredMindmapPersisters =
            new Dictionary<Guid, FileSystemPersister>();

        protected static IMindmapService MindmapService
        {
            get { return BookvarApplication.Current.ServiceLayer.GetService<IMindmapService>(); }
        }

        #region IPersistenceService Members

        public event EventHandler<SavingEventArgs> Saving;

        public bool SaveMapToPersistence( MindMap map )
        {
            if ( registeredMindmapPersisters.ContainsKey( map.Key ) )
            {
                OnSaving( new SavingEventArgs( map ) );
                return registeredMindmapPersisters[ map.Key ].Save();
            }
            return false;
        }

        public MindMap LoadMapFromPersistence( Guid mapId )
        {
            if ( registeredMindmapPersisters.ContainsKey( mapId ) )
            {
                return registeredMindmapPersisters[ mapId ].Load();
            }
            return null;
        }

        public void SaveFileResourceToPersistence( Guid mapId, IFileResource resource, Stream resourceStream )
        {
            if ( registeredMindmapPersisters.ContainsKey( mapId ) )
            {
                registeredMindmapPersisters[ mapId ].SaveFileResource( resource, resourceStream );
            }
        }

        public Stream LoadFileResourceFromPersistence( Guid mapId, IFileResource resource )
        {
            if ( registeredMindmapPersisters.ContainsKey( mapId ) )
            {
                return registeredMindmapPersisters[ mapId ].LoadFileResource( resource );
            }
            return null;
        }

        public void RemoveFileResourceFromPersistence( Guid mapId, IFileResource resource )
        {
            if ( registeredMindmapPersisters.ContainsKey( mapId ) )
            {
                registeredMindmapPersisters[ mapId ].RemoveFileResource( resource );
            }
        }

        #endregion

        public void RegisterMindmapForPersistence( MindMap map )
        {
            if ( !registeredMindmapPersisters.ContainsKey( map.Key ) )
            {
                registeredMindmapPersisters.Add( map.Key, new FileSystemPersister( map ) );
            }
        }

        public void RegisterMindmapForPersistence( MindMap map, string fileName )
        {
            if ( !registeredMindmapPersisters.ContainsKey( map.Key ) )
            {
                registeredMindmapPersisters.Add( map.Key, new FileSystemPersister( map, fileName ) );
            }
            else
            {
                registeredMindmapPersisters[ map.Key ].FileName = fileName;
            }
        }

        // TODO: REFACTOR after the persistence layer is improved.
        public MindMap GetLoadedMapByFilePath( string filePath )
        {
            foreach ( MindMap mindMap in MindmapService.LoadedMindMaps )
            {
                FileSystemPersister fileSystemPersister = registeredMindmapPersisters[ mindMap.Key ];
                if ( string.IsNullOrEmpty( fileSystemPersister.FileName ) == false )
                {
                    if ( string.Compare( Path.GetFullPath( filePath ),
                                         Path.GetFullPath( fileSystemPersister.FileName ),
                                         StringComparison.OrdinalIgnoreCase ) == 0 )
                    {
                        return mindMap;
                    }
                }
            }

            return null;
        }

        public Uri LoadThumbnailFromPersistence( Guid mapId )
        {
            if ( registeredMindmapPersisters.ContainsKey( mapId ) )
            {
                return registeredMindmapPersisters[ mapId ].LoadThumbnail();
            }
            return null;
        }

        protected virtual void OnSaving( SavingEventArgs e )
        {
            if ( Saving != null )
            {
                Saving( this, e );
            }
        }
    }
}